﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace NewBee.Core.Data.SQLBuilder
{
    public class Select
    {
        private static string SELECT_PAGE_SQL = "SELECT {0} * FROM ( SELECT ROW_NUMBER() OVER ({1}) AS ROW , {2} FROM {3} WHERE {4} ) AS DATALIST WHERE ROW > {5}";
        private static string SELECT_SQL = @"SELECT {1} FROM {0} WHERE {2}";
        private DbHelper _DbHelper = null;  //数据操作类
        private string _Columns = " * ";  //列名称/值 的集合
        private string _Where = string.Empty;
        private string _OrderBy = string.Empty;
        private string _From = string.Empty;
        private object _Parameters;

        public Select(DbHelper dbHelper, string colums)
        {
            _DbHelper = dbHelper;
            _Columns = colums;
        }
    }
    public class Select_T<T>
    {
        private static string SELECT_PAGE_SQL = "SELECT {0} * FROM ( SELECT ROW_NUMBER() OVER ({1}) AS ROW , {2} FROM {3} WHERE {4} ) AS DATALIST WHERE ROW > {5}";
        private static string SELECT_SQL = @"SELECT {1} FROM {0} WHERE {2}";
        private DbHelper _DbHelper = null;  //数据操作类
        private int _Rows = 0;   //一页多少条
        private int _Page = 0;   //第几页
        private string _TableName = string.Empty;   //表名
        private string _Columns = " * ";  //列名称/值 的集合
        private string _Where = string.Empty;
        private string _OrderBy = string.Empty;
        private string _From = string.Empty;
        private object _Parameters;

        public Select_T(DbHelper dbHelper, string tableName,string colums)
        {
            _DbHelper = dbHelper;
            _TableName = tableName;
            _Columns = colums;
        }

        public Select_T<T> Where(string where)
        {
            _Where = where;
            return this;
        }

        public Select_T<T> OrderBy(string orderBy)
        {
            _OrderBy = orderBy;
            return this;
        }

        public Select_T<T> From(string from)
        {
            _From = from;
            return this;
        }

        public Select_T<T> Page(int page,int rows)
        {
            _Page = page;
            _Rows = rows;
            return this;
        }


        public IEnumerable<T> Query()
        {
            string sql = string.Empty;
            if (_Rows > 0 && _Page > 0)
            {
                //分页
                sql = string.Format(SELECT_PAGE_SQL, _Rows, _OrderBy, _Columns, _TableName, _Where, _Page);
            }
            else
            {
                if (string.IsNullOrEmpty(_From))
                    _From = _TableName;

                if (string.IsNullOrEmpty(_Where))
                    _Where = " 1=1 ";

                if (!string.IsNullOrEmpty(_OrderBy))
                    _Where += " Order By" + _OrderBy;

                sql = string.Format(SELECT_SQL, _Columns, _From, _Where);
            }
            return _DbHelper.Query<T>(sql, _Parameters);
        }
    }
}
